#include "login_protocol.hpp"
#include "../core/di_container.hpp"
#include "../common/logger.hpp"
#include "../core/game_server.hpp"

namespace protocols {

LoginProtocol::LoginProtocol() {
    Logger::info("LoginProtocol created");
    user_service_ = DIContainer::get<service::UserService>();
}

LoginProtocol::~LoginProtocol() {
    Logger::info("LoginProtocol destroyed");
}

bool LoginProtocol::validate(GameServer& game_server, const nlohmann::json& message, uint32_t session_id) {
    return true;
}

void LoginProtocol::process(GameServer& game_server, const nlohmann::json& message, uint32_t session_id) {
    Logger::info("LoginProtocol process");

        if (user_service_->login_user(message["username"].get<std::string>(), message["password"].get<std::string>())) {
        Logger::info("Login success");

        // 通知登录成功
        nlohmann::json login_success_message = {
            {"type", "login"},
            {"status", 100},
            {"message", "login success"}
        };
        game_server.send_message(session_id, login_success_message);
    } else {
        // 通知登录失败
        nlohmann::json login_failed_message = {
            {"type", "login"},
            {"status", 101},
            {"message", "login failed"}
        };
        game_server.send_message(session_id, login_failed_message);
        Logger::info("Login failed");
    }
}

} // namespace protocols